<script setup lang="ts">
import CustomNavbar from './components/CustomNavbar.vue'
import { onLoad } from '@dcloudio/uni-app'
import { getHomeBannerAPI, getCategoryMutliAPI, getHomeHotAPI } from '@/services/home'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import { ref } from 'vue'
import CategoryPanel from './components/CategoryPanel.vue'
import hotPanel from './components/HotPanel.vue'
import type { XtxGuessInstance } from '@/types/component'

let bannerList = ref<BannerItem[]>([])
let categoryList = ref<CategoryItem[]>([])
let hotList = ref<HotItem[]>([])

const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
}
// 父组件调用子组件的数据
const guessRef = ref<XtxGuessInstance>()

const onScrolltolower = () => {
  console.log('到此了')
  guessRef.value?.getMore()
}

const getCategoryMutliData = async () => {
  const res = await getCategoryMutliAPI()
  categoryList.value = res.result
}
const getHotData = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}
const isTriggered = ref(false)
const onRefresherrefresh = async () => {
  // 开启动画
  isTriggered.value = true
  guessRef.value?.resetData()
  await Promise.all([getHomeBannerData(), getCategoryMutliData(), getHotData()])
  isTriggered.value = false
}
onLoad(() => {
  getHomeBannerData()
  getCategoryMutliData()
  getHotData()
})
</script>

<template>
  <CustomNavbar></CustomNavbar>
  <scroll-view
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"
    @scrolltolower="onScrolltolower"
    scroll-y
    class="scroll-view"
  >
    <XtxSwiper :list="bannerList"></XtxSwiper>
    <CategoryPanel :list="categoryList"></CategoryPanel>
    <hotPanel :list="hotList"></hotPanel>
    <XtxGuess ref="guessRef"></XtxGuess>
  </scroll-view>
</template>

<style lang="scss">
//
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
